package charts.series.bars ;
import flash.filters.DropShadowFilter;
import flash.geom.Matrix;
import charts.series.bars.Base;
	
	
class Round extends Base
{
	
	public function new ( index: Int, style:Dynamic, group: Float ) {
	
	
	super(index, style, style.color, style.tip, style.alpha, group);
	
	var dropShadow:DropShadowFilter = new flash.filters.DropShadowFilter();
	dropShadow.blurX = 5;
	dropShadow.blurY = 5;
	dropShadow.distance = 3;
	dropShadow.angle = 45;
	dropShadow.quality = 2;
	dropShadow.alpha = 0.4;
	// apply shadow filter
	this.filters = [dropShadow];
	}
	
	public override function resize ( sc:ScreenCoordsBase ):Void {
	
	this.graphics.clear();
	var h:Dynamic = this.resize_helper( cast(sc,ScreenCoords));
	
	this.bg( h.width, h.height, h.upside_down );
	this.glass( h.width, h.height, h.upside_down );
	}
	
	function bg ( w: Float, h: Float, upside_down:Bool ):Void {

	var rad: Float = w/3;
	if ( rad > ( w / 2 ) )
	rad = w / 2;
	
	this.graphics.lineStyle(0, 0, 0);// this.outline_color, 100);
	
	var bgcolors:Array<Float> = getColors(this.color);
	var bgalphas:Array<Float> = [1, 1];
	var bgratios:Array<Int> = [0, 255];
	var bgmatrix:Matrix = new Matrix();
	var xRadius: Float;
	var yRadius: Float;
	var x: Float;
	var y: Float;
	
	bgmatrix.createGradientBox(w, h, (180 / 180) * Math.PI );
	this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, bgcolors, bgalphas, bgratios, bgmatrix, 'pad'/*SpreadMethod.PAD*/ );
	
	if (h > 0)
	{
		
		if ( h >= w)
		{ /* bar is tall enough for normal draw */
		
		/* draw bottom half ellipse */
		x = w/2;
		y = h - w / 2;
		xRadius = w / 2;
		yRadius = xRadius;
		halfEllipse(x, y, xRadius, yRadius, 100, false);
				
		/* draw connecting rectangle */
		this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, bgcolors, bgalphas, bgratios, bgmatrix, 'pad'/*SpreadMethod.PAD*/ );
		this.graphics.moveTo(0, w/2);
		this.graphics.lineTo(0, h - w/2);
		this.graphics.lineTo(w, h - w/2);
		this.graphics.lineTo(w, w / 2);   
			
		/* draw top ellipse */
		//this.graphics.beginFill(this.color, 1);
		x = w / 2;
		y = w / 2;
		xRadius = w / 2;
		yRadius = xRadius;
		halfEllipse(x, y, xRadius, yRadius, 100, true);
		
		}
		
		else
		
		{ /* bar is too short for normal draw */               
		
		/* draw bottom half ellipse */
		x = w/2;
		y = h / 2;
		xRadius = w / 2;
		yRadius = h / 2;
		halfEllipse(x, y, xRadius, yRadius, 100, false);   
			
		/* draw top ellipse */
		x = w / 2;
		y = h / 2;
		xRadius = w / 2;
		yRadius = h / 2;
		halfEllipse(x, y, xRadius, yRadius, 100, true);
		
		}
	}
	
	else
	
	{
		
		{ /* bar is zero */
		
		/* draw top ellipse */
		x = w/2;
		y = h;
		xRadius = w / 2;
		yRadius = rad / 4;
		ellipse(x, y, xRadius, yRadius, 100);         
		
		}
		
	}
	
	this.graphics.endFill();
	}
	
	function glass ( w: Float, h: Float, upside_down:Bool ): Void {
	
	/* if this section is commented out, the white shine overlay will not be drawn */
	
	this.graphics.lineStyle(0, 0, 0);
	var bgcolors:Array<Float> = getColors(this.color);
	var bgalphas:Array<Float> = [1, 1];
	var bgratios:Array<Int> = [0, 255];
	var bgmatrix:Matrix = new Matrix();
	
	bgmatrix.createGradientBox(w, h, (180 / 180) * Math.PI );
	
	/* set gradient fill */
	var colors:Array<Float> = [0xFFFFFF, 0xFFFFFF];
	var alphas:Array<Float> = [0, 0.75];
	var ratios:Array<Int>  = [100,255];         
	var xRadius: Float;
	var yRadius: Float;
	var x: Float;
	var y: Float;
	var matrix:Matrix = new Matrix();
	matrix.createGradientBox(width, height, (180 / 180) * Math.PI );
	this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, colors, alphas, ratios, matrix, 'pad'/*SpreadMethod.PAD*/ );
	var rad: Float = w / 3;
	
	if (h > 0)
	{ /* draw shine upward */
	
		if (h >= w)
		{ /* bar is tall enough for normal draw */
	
		/* draw bottom half ellipse shine */
		this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, colors, alphas, ratios, matrix, 'pad'/*SpreadMethod.PAD*/ );
		x = w / 3;
		y = h - w / 2;
		xRadius = w / 3 - (0.05 * w);
		yRadius = xRadius + (0.05 * w);
		halfEllipse(x, y, xRadius, yRadius, 100, false);
		
		/*draw connecting rectangle shine */
		this.graphics.moveTo(0 + (0.05 * w), w/2);
		this.graphics.lineTo(0 + (0.05 * w), h - w/2);
		this.graphics.lineTo(w - (0.05 * w), h - w/2);
		this.graphics.lineTo(w - (0.05 * w), w/2);   
		
		/* draw top ellipse shine */
		this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, colors, alphas, ratios, matrix, 'pad'/*SpreadMethod.PAD*/ );
		x = w / 3;
		y = w / 2;
		xRadius = w / 3 - (0.05 * w);
		yRadius = xRadius + (0.05 * w);
		halfEllipse(x, y, xRadius, yRadius, 100, true);
		
		}
		
		else
		
		{ /* bar is not tall enough for normal draw */   
		
		this.graphics.beginGradientFill('linear' /*GradientType.Linear*/, colors, alphas, ratios, matrix, 'pad'/*SpreadMethod.PAD*/ );
		x = w / 3;
		y = h / 2;
		xRadius = w / 3 - (0.05 * w);
		yRadius = h/2 - 3*(0.05 * w);
		ellipse(x, y, xRadius, yRadius, 100);
		
		}
	
	}
	
	else
	
	{ /* bar is zero */
		
		/* draw top ellipse shine*/
		x = w/2;
		y = h;
		xRadius = w / 2 - (0.05 * w);
		yRadius = rad / 4 - (0.05 * w);
		ellipse(x, y, xRadius, yRadius, 100);   
			
	}
		
	this.graphics.endFill();
	}
	
	public static function getColors ( col: Int ):Array<Int> {
		var rgb: Int = col; /* decimal value for color */
		var red: Int = (rgb & 16711680) >> 16; /* extacts the red channel */
		var green: Int = (rgb & 65280) >> 8; /* extacts the green channel */
		var blue: Int = rgb & 255; /* extacts the blue channel */
		var shift: Int = 2; /* shift factor */
		var basecolor: Int = col; /* base color to be returned */
		var highlight: Int = col; /* highlight color to be returned */
		var bgred: Int = (rgb & 16711680) >> 16; /* red channel for highlight */
		var bggreen: Int = (rgb & 65280) >> 8; /* green channel for highlight */
		var bgblue: Int = rgb & 255; /* blue channel for highlight */
		var hired: Int = (rgb & 16711680) >> 16; /* red channel for highlight */
		var higreen: Int = (rgb & 65280) >> 8; /* green channel for highlight */
		var hiblue: Int = rgb & 255; /* blue channel for highlight */
		
		/* set base color components based on ability to shift lighter */
		var red_shift : Int = Std.int(red / shift);
		var green_shift : Int = Std.int(green / shift);
		var blue_shift : Int = Std.int(blue / shift);

		if (red + red_shift > 255 || green + green_shift > 255 || blue + blue_shift > 255)
		{
			bgred = red - red_shift;
			bggreen = green - green_shift;
			bgblue = blue - blue_shift;
		}            
		
		/* set highlight components based on base colors */   
		hired = bgred + red_shift;
		hiblue = bgblue + blue_shift;
		higreen = bggreen + green_shift;
		
		/* reinlineruct base and highlight */
		basecolor = bgred << 16 | bggreen << 8 | bgblue;
		highlight = hired << 16 | higreen << 8 | hiblue;
			
		/* return base and highlight */
		return [highlight, basecolor];
	}
	
	/* ellipse cos helper function new*/
	public static function magicTrigFunctionX (pointRatio: Float): Float{
	return Math.cos(pointRatio*2*Math.PI);
	}
	
	/* ellipse sin helper function new*/
	public static function magicTrigFunctionY (pointRatio: Float): Float{
	return Math.sin(pointRatio*2*Math.PI);
	}
	
	/* ellipse function new*/
	/* draws an ellipse from passed center coordinates, x and y radii, and number of sides */
	public function ellipse (centerX: Float, centerY: Float, xRadius: Float, yRadius: Float, sides: Int): Float{
	
	/* move to first point on ellipse */
	this.graphics.moveTo(centerX + xRadius,  centerY);
	
	/* loop through sides and draw curves */
	for ( i in 0...(sides+1) ){
	var pointRatio: Float = i/sides;
	var xSteps: Float = magicTrigFunctionX(pointRatio);
	var ySteps: Float = magicTrigFunctionY(pointRatio);
	var pointX: Float = centerX + xSteps * xRadius;
	var pointY: Float = centerY + ySteps * yRadius;
	this.graphics.lineTo(pointX, pointY);
	}
	
	return 1;
	}
	
	/* half ellipse function new*/
	/* draws half of an ellipse from passed center coordinates, x and y radii, number of sides , and top/bottom */
	public function halfEllipse (centerX: Float, centerY: Float, xRadius: Float, yRadius: Float, sides: Float, top:Bool): Void {
		
		var loopStart: Float;
		var loopEnd: Float;
		
		
		if (top == true)
		{ /* drawing top half of ellipse */
		
			loopStart = sides / 2;
			loopEnd = sides;
			
		}
		else
		{ /* drawing bottom half of ellipse */
			
			loopStart = 0;
			loopEnd = sides / 2;   
			
		}
		
		/* move to first point on ellipse */
		this.graphics.moveTo(centerX + xRadius,  centerY);
		
		/* loop through sides and draw curves */
		//for ( i in loopStart...(loopEnd+1) ){
		var i : Float = loopStart;
		while (i <= loopEnd)
		{
			var pointRatio: Float = i/sides;
			var xSteps: Float = magicTrigFunctionX(pointRatio);
			var ySteps: Float = magicTrigFunctionY(pointRatio);
			var pointX: Float = centerX + xSteps * xRadius;
			var pointY: Float = centerY + ySteps * yRadius;
			this.graphics.lineTo(pointX, pointY);
		}
		
		/* return 1 */
	}
	
}
